define(["Echarts"],function (Echarts) {
    var report = {};

    /* 柱状图 */
    report.createBarEcharts = function (obj) {
        var myBarChart = Echarts.init(document.getElementById(obj.id));
        var option = {
            color:["#37A2DA","#32C5E9","#67E0E3","#9FE6B8","#FFDB5C","#FF9F7F","#FB7293","#E062AE","#E690D1","#E7BCF3","#9D96F5","#8378EA","#96BFFF", "#7D9CCF", "#87C896", "#C5DC87"],
            title: {
                text:obj.title,
                subtext:obj.subtitle,
                left:'center',
                textStyle:{
                    color:'#666',
                    /*fontStyle:'normal',
                    fontWeight:'bold',*/
                    //字体大小
                    fontSize:20
                },
                subtextStyle:{
                    color:'#9f999f',
                    fontWeight:'bold',
                    fontSize:14
                }
            },
            tooltip: {
                trigger: 'axis',
                /*formatter:function(params, ticket, callback){
                    console.log(params);
                    console.log(ticket);
                }*/
            },
            grid: {
                left: '3%',
                bottom: '3%',
                containLabel: true
            },
            yAxis: {
                type: 'category',
                data: obj.x
            },
            xAxis: {
                type: 'value',
                position: 'bottom',
                splitLine: {
                    lineStyle: {
                        type: 'dashed'
                    }
                }
            },
            series: [{
                data: obj.data,
                type: 'bar',
                stack: 'total',
                animation: false,
                itemStyle: {
                    normal: {
                        label: {
                            show: true, //开启显示
                            position: 'right', //在右方显示
                            textStyle: { //数值样式
                                color: 'black',
                                fontSize: 16
                            }
                        }
                    }
                }
            }]
        };
        myBarChart.setOption(option,true);

        imgbase64(obj.id);
    }

    /* 饼状图 */
    report.createPieEcharts = function(obj){
        var myPieChart = Echarts.init(document.getElementById(obj.id));
        var total = obj.series.data.reduce(function (a, b) { return a+b.value}, 0);
        var data = obj.series.data.map((item)=>{
            if(item.value/total<0.1 || item.value == 0){
                item.itemStyle = {
                    normal:{
                        label:{
                            show: true,
                            position: 'outside',
                            formatter: '{d}%'
                        },
                        labelLine :{show:true}
                    }
                }
            }
            return item
        })
        if(total == 0){
            $("#"+obj.id).hide();
        }
        var series = {
            name: obj.series.name,
            type: 'pie',
            radius: '60%',
            center: ['50%', '50%'],
            selectedMode: 'single',
            animation: false,
            itemStyle:{
                normal:{
                    label:{
                        show: true,
                        position: 'inside',
                        formatter: '{d}%'
                    },
                    labelLine :{show:true},
                    borderWidth: 1,
                    borderColor: '#fff'
                },
            },
            data: data
        };
        var option = {
            // color:["#37A2DA","#32C5E9","#67E0E3","#9FE6B8","#FFDB5C","#FF9F7F","#FB7293","#E062AE","#E690D1","#E7BCF3","#9D96F5","#8378EA","#96BFFF", "#7D9CCF", "#87C896", "#C5DC87"],
            color:["rgb(164,5,69)","rgb(244,111,68)","rgb(253,217,133)","rgb(233,245,161)","rgb(127,203,164)","rgb(75,101,175)","rgb(144,201,231)","rgb(33,158,188)","rgb(19,103,131)","rgb(2,48,74)","rgb(254,183,5)","rgb(258,158,2)","rgb(250,134,0)", "#7D9CCF", "#87C896", "#C5DC87"],
            title: {
                text: obj.title,
                subtext: obj.subtitle,
                left: 'center',
                textStyle: { // 主标题的属性
                    fontSize: 30,//大小
                },
                subtextStyle: { //副标题的属性
                    color: '#25664A',
                    // 同主标题
                    fontSize: 25,//大小
                    fontWeight: '700'//粗细
                }
            },
            tooltip: {
                trigger: 'item',
                formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            legend: {
                left: 'center',
                top: 'bottom',
                data: obj.series.data.map(item => item.name),
            },
            // toolbox: {
            //     show: true,
            //     feature: {
            //         mark: {show: true},
            //         dataView: {show: true, readOnly: false},
            //         magicType: {
            //             show: true,
            //             type: ['pie', 'funnel']
            //         },
            //         restore: {show: true},
            //         saveAsImage: {show: true}
            //     }
            // },
            series: series
        };

        myPieChart.setOption(option,true);

        imgbase64(obj.id);
    }

    //将canvas格式图片转换为图片
    var getFullCanvasDataURL = function(divId){
        //将第一个画布作为基准。
        var baseCanvas = $("#"+divId).find("canvas").first()[0];
        if(!baseCanvas){
            return false;
        };
        var width = baseCanvas.width;
        var height = baseCanvas.height;
        var ctx = baseCanvas.getContext("2d");
        //遍历，将后续的画布添加到在第一个上
        $("#"+divId).find("canvas").each(function(i,canvasObj){
            if(i>0){
                var canvasTmp = $(canvasObj)[0];
                ctx.drawImage(canvasTmp,0,0,width,height);
            }
        });
        //获取base64位的url
        return baseCanvas.toDataURL("image/png");
    }

    // 将echart的图保存成图片
    var imgbase64 = function(divId) {
        var picInfo = getFullCanvasDataURL(divId);
        $.ajax({
            type: "post",
            url: getServer() + '/fw/pcReport/image',
            data: {base64Info: picInfo, fileName: divId, timeStamp: report.timeStamp},
            async: "false",
            dataType: "json",
            success: function (data) {
                // $("#"+divId+"_img").attr('src', data.imageUrl+'?temp=' + Math.random());
                // $("#"+divId).hide();
            }
        });
    }

    return report;
})